package vn.keho.module.dompeto.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bingkaimedia.merchantbeta.R;
import com.mobeta.android.dslv.DragSortController;

import java.util.ArrayList;
import java.util.List;

import vn.keho.module.dompeto.MerchantActivity;
import vn.keho.module.dompeto.model.ChannelModel;


/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link PageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PageFragment extends Fragment {

    private int fragmentPage = 0;
    private int fragmentId;

    private List<ChannelModel> listChannelData = new ArrayList<ChannelModel>();

    // TODO: Rename and change types and number of parameters
    public static PageFragment newInstance(int fragmentPage, List<ChannelModel> listChannelModel) {
        PageFragment fragment = new PageFragment();
        fragment.fragmentPage = fragmentPage;
        if(listChannelModel != null) {
            fragment.listChannelData = listChannelModel;
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*if (article == null && (savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            article = JsonUtil.generateJavaObject(savedInstanceState.getString(KEY_CONTENT), Article.class);
        }*/
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_page, container, false);
        String resourceName = "fragmentPage" + fragmentPage;
        fragmentId = getResources().getIdentifier(resourceName, "id", getActivity().getPackageName());
        view.setId(fragmentId);

        if (savedInstanceState == null) {
            getActivity().getSupportFragmentManager().beginTransaction().add(fragmentId, getNewDslvFragment(), mTag).commit();
        }
        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        /*if (article != null) {
            outState.putString(KEY_CONTENT, JsonUtil.buildJsonString(article));
        }*/
    }

    private String mTag = "dslvTag";
    private DSLVFragmentClicks sortFragment;

    private DSLVFragmentClicks getNewDslvFragment() {
        boolean isEditMode = MerchantActivity.getInstance().isEditMode;
        int removeMode = DragSortController.FLING_REMOVE;
        if (isEditMode) {
            removeMode = DragSortController.CLICK_REMOVE;
        }
        sortFragment = DSLVFragmentClicks.newInstance(listChannelData);
        sortFragment.removeMode = removeMode;
        sortFragment.removeEnabled = isEditMode;
        sortFragment.dragStartMode = DragSortController.ON_DRAG;
        sortFragment.sortEnabled = true;
        sortFragment.dragEnabled = isEditMode;

        return sortFragment;
    }

    public void updateEditMode() {
        getActivity().getSupportFragmentManager().beginTransaction().replace(fragmentId, getNewDslvFragment(), mTag).commit();
    }

    public ChannelModel removeChannel(int removeIndex) {
        ChannelModel channelModel = listChannelData.get(removeIndex);
        WalletDragSortListView walletDragSortListView = (WalletDragSortListView) sortFragment.getListView();
        walletDragSortListView.stopDragWithVelocity(true, 0);
        return channelModel;
    }

    public void clearChannel(int replaceIndex) {
        listChannelData.remove(replaceIndex);
        listChannelData.add(replaceIndex, new ChannelModel("false", replaceIndex + 1));
        getActivity().getSupportFragmentManager().beginTransaction().replace(fragmentId, getNewDslvFragment(), mTag).commit();
    }

    public void addChannel(ChannelModel channelModel, int replaceIndex) {
        listChannelData.remove(replaceIndex);
        listChannelData.add(replaceIndex, channelModel);
        getActivity().getSupportFragmentManager().beginTransaction().replace(fragmentId, getNewDslvFragment(), mTag).commit();
    }

    public int findSlotAvailable(ImageView imgDragDrop) {
        WalletDragSortListView walletDragSortListView = (WalletDragSortListView) sortFragment.getListView();
        int touchPos = walletDragSortListView.pointToPosition((int)(imgDragDrop.getX() + imgDragDrop.getWidth()/2), (int)(imgDragDrop.getY() + imgDragDrop.getHeight()/2)); // includes headers/footers
        if(touchPos != AdapterView.INVALID_POSITION) {
            ChannelModel channelModel = listChannelData.get(touchPos);
            if(!channelModel.getChannelid().equals("false")) {
                return AdapterView.INVALID_POSITION;
            }
        }
        return touchPos;
    }

    public int findEmptySlot() {
        for(int i = 0; i < listChannelData.size();i++) {
            ChannelModel channelModel = listChannelData.get(i);
            if(channelModel.getChannelid().equals("false")) {
                return i;
            }
        }
        return AdapterView.INVALID_POSITION;
    }

    public ChannelModel findChannelIndex(int removeIndex) {
        return listChannelData.get(removeIndex);
    }

    /**
     * Listener from Fragment Activity
     */
    public interface OnPageFragmentListener {

    }
}
